package edu.ssm.campusAssistant.aop;

import edu.ssm.campusAssistant.bo.rsp.RspBO;
import edu.ssm.campusAssistant.constant.RspConstants;
import edu.ssm.campusAssistant.exception.BusinessException;
import edu.ssm.campusAssistant.exception.ParametersException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 标题:
 * 描述:
 *
 * @autho LXF
 * @date 2019/4/8 16:31
 */
public class ExceptionAdvisor implements Ordered {
    private final static Logger logger = LoggerFactory.getLogger(ExceptionAdvisor.class);
    private static final boolean isDebugEnabled = logger.isDebugEnabled();

    @Override
    public int getOrder() {
        return 0;
    }

    public Object throwAopTreat(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        Method method = methodSignature.getMethod();
        Class<?> rspClass = method.getReturnType();
        Object rspObj = null;
        try {
            rspObj = pjp.proceed();
            this.putCodeToRsp(rspObj, RspConstants.RSP_CODE_SUCCESS, RspConstants.RSP_DESC_SUCCESS);
        } catch (BusinessException e) {
            rspObj = rspClass.newInstance();
            String msgCode = e.getMsgCode();
            String desc = e.getMessage();
            if (msgCode == null || "".equals(msgCode)) {
                if (e.getCause() instanceof BusinessException) {
                    msgCode = ((BusinessException) e).getMsgCode();
                }
                if (msgCode == null || "".equals(msgCode)) {
                    msgCode = RspConstants.RSP_CODE_FAILUR;
                }
            }
            logger.error("业务服务编码：" + msgCode + ",业务服务编码描述：" + desc);
            this.putCodeToRsp(rspObj, msgCode, desc);
        } catch (ParametersException e) {
            rspObj = rspClass.newInstance();
            String msgCode = e.getMsgCode();
            String desc = e.getMessage();
            if (msgCode == null || "".equals(msgCode)) {
                if (e.getCause() instanceof BusinessException) {
                    msgCode = ((ParametersException) e).getMsgCode();
                }
                if (msgCode == null || "".equals(msgCode)) {
                    msgCode = RspConstants.RSP_CODE_FAILUR;
                }
            }
            logger.error("业务服务编码：" + msgCode + ",业务服务编码描述：" + desc);
            this.putCodeToRsp(rspObj, msgCode, desc);
        } catch (Throwable e) {
            logger.error("系统异常  ", e);
            rspObj = rspClass.newInstance();
            this.putCodeToRsp(rspObj, RspConstants.RSP_CODE_FAILUR, RspConstants.RSP_DESC_FAILUR);
        }
        if (isDebugEnabled) {
            logger.debug("业务服务切面响应报文：" + rspObj.toString());
        }
        return rspObj;
    }

    private void putCodeToRsp(Object object, String code, String desc) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
        if (object instanceof RspBO) {
            Field field = RspBO.class.getDeclaredField("respCode");
            // 如果业务中已经加入的返回值，则不予以赋值
            field.setAccessible(true);
            if (field.get(object) == null) {
                field.set(object, code);
            }
            field = RspBO.class.getDeclaredField("respDesc");
            field.setAccessible(true);
            if (field.get(object) == null) {
                field.set(object, desc);
            }
        }
    }

}
